package yhao.infra.feature.alipay.core.request.refund;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.apache.commons.lang3.StringUtils;
import yhao.infra.feature.alipay.core.request.BaseRefundRequest;

/**
 * 统一收单交易退款接口
 * @see <a href="https://docs.open.alipay.com/api_1/alipay.trade.refund/">更多细节</a>
 */
public class RefundRequest extends BaseRefundRequest<AlipayTradeRefundResponse> {
    private String trade_no;//支付时返回的支付宝交易号，与out_trade_no必填一个
    private String refund_amount;//本次退款金额
    private String reason;         //原因

    public RefundRequest(String out_trade_no, String trade_no, String out_request_no, String refund_amount) {
        super(out_trade_no,out_request_no);
        this.trade_no = trade_no;
        this.refund_amount = refund_amount;
    }

    public String getTrade_no() {
        return trade_no;
    }

    public void setTrade_no(String trade_no) {
        this.trade_no = trade_no;
    }

    public String getRefund_amount() {
        return refund_amount;
    }

    public void setRefund_amount(String refund_amount) {
        this.refund_amount = refund_amount;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    @Override
    protected AlipayTradeRefundResponse doRequest() {
        AlipayTradeRefundModel refundModel = new AlipayTradeRefundModel();
        refundModel.setOutTradeNo(this.getOut_trade_no());
        refundModel.setTradeNo(this.getTrade_no());
        refundModel.setRefundAmount(this.getRefund_amount());
        refundModel.setRefundReason(StringUtils.isEmpty(this.getReason())?"退款":this.getReason());
        refundModel.setOutRequestNo(this.getOut_request_no());
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();//创建API对应的request类
        request.setBizModel(refundModel);//设置业务参数

        String method = logRequest(request);
        try {
            return alipayClient.execute(request);//通过alipayClient调用API，获得对应的response类
        } catch (AlipayApiException e) {
            logger.error(method+getOut_trade_no(), e.getMessage());
        }
        return null;
    }
}
